const path = require("path");
const defaultConfig = require("../template/webpack/webpack.config.default");
const { merge } = require("webpack-merge");
const webpack = require("webpack")
const WebpackDevServer = require("webpack-dev-server");

function build(){
    // 1. 读取用户的配置文件
    const userConfig = require(path.join(process.cwd(), 'hm.config')) || {}
    // 2. 合并默认配置和用户配置
    const config = merge(defaultConfig, {
        mode: 'production',
        devtool: userConfig.devtool || 'source-map'
    })
    // 3. 生成webpack编译器实例
    const compiler = webpack(config)
    // 4. 执行打包
    compiler.run((err,stats)=>{
        if(err){
            console.log('打包出错:',err)
        }
        // 销毁webpack编译实例
        compiler.close((err,result)=>{
            if(err) console.log("销毁失败",err);
        });
    })
}


module.exports = {
    build
}